package com.yedou.handler;

import com.yedou.common.SubjectInfoTypeEnum;
import com.yedou.convert.JudgeSubjectConverter;
import com.yedou.entity.bo.SubjectAnswerBO;
import com.yedou.entity.bo.SubjectInfoBO;
import com.yedou.entity.bo.SubjectOptionBO;
import com.yedou.entity.po.SubjectJudge;
import com.yedou.service.SubjectJudgeService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Component
public class JudgeTypeHandler implements SubjectTypeHandler{

    @Resource
    private SubjectJudgeService subjectJudgeService;
    
    @Override
    public SubjectInfoTypeEnum getHandlerType() {
        return SubjectInfoTypeEnum.JUDGE;
    }

    @Override
    public void add(SubjectInfoBO subjectInfoBO) {
        List<SubjectJudge> subjectJudgeList = new LinkedList<>();
        // 判断题目的插入
        subjectInfoBO.getOptionList().forEach(option ->{
            SubjectJudge subjectJudge = new SubjectJudge();
            subjectJudge.setSubjectId(subjectInfoBO.getId());
            subjectJudge.setIsCorrect(option.getIsCorrect());
            subjectJudge.setIsDeleted(0);
            subjectJudgeList.add(subjectJudge);
        });
//        SubjectAnswerBO subjectAnswerBO = subjectInfoBO.getOptionList().get(0);
//        subjectJudge.setSubjectId(subjectInfoBO.getId());
//        subjectJudge.setIsCorrect(subjectAnswerBO.getIsCorrect());
//        subjectJudge.setIsDeleted(0);
        subjectJudgeService.batchInsert(subjectJudgeList);
    }

    @Override
    public SubjectOptionBO query(int subjectId) {
        SubjectJudge subjectJudge = new SubjectJudge();
        subjectJudge.setSubjectId(Long.valueOf(subjectId));
        List<SubjectJudge> result = subjectJudgeService.queryByCondition(subjectJudge);
        List<SubjectAnswerBO> subjectAnswerBOList = JudgeSubjectConverter.INSTANCE.convertEntityToBoList(result);
        SubjectOptionBO subjectOptionBO = new SubjectOptionBO();
        subjectOptionBO.setOptionList(subjectAnswerBOList);
        return subjectOptionBO;
//        return null;
    }
}
